﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using Delta.Engine;

public partial class DeltaView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!IsPostBack)
        //{
        //    MemoryStream ms = new MemoryStream();
        //    StreamWriter sw = new StreamWriter(ms);
        //    sw.WriteLine("Stream Line 1");
        //    sw.WriteLine("Stream Line 2");
        //    sw.WriteLine("Stream Line 2");
        //    sw.WriteLine("Stream Line 3");
        //    sw.WriteLine("Stream Line 3");
        //    sw.WriteLine("Stream Line 3");
        //    sw.Flush();

        //    ms.Seek(0, SeekOrigin.Begin);

        //    ContentWrapper map1 = new ContentWrapper(ms);

        //    ms = new MemoryStream();
        //    sw = new StreamWriter(ms);
        //    sw.WriteLine("Stream Line 1");
        //    sw.WriteLine("Stream Line 2");
        //    sw.WriteLine("Stream Line 2");
        //    sw.WriteLine("Stream Line 4");
        //    sw.WriteLine("Stream Line 3");
        //    sw.WriteLine("Stream Line 3");
        //    sw.WriteLine("Stream Line 3");
        //    sw.Flush();

        //    ms.Seek(0, SeekOrigin.Begin);

        //    ContentWrapper map2 = new ContentWrapper(ms);

        //    ProcessStreams(map1, map2);
        //}
    }

    private void ProcessStreams(ContentWrapper map1, ContentWrapper map2)
    {
        Processor processor = new Processor();

        processor.Process(map1, map2);

        List<DeltaResult> results = processor.Result();

        var map = from r in results
             select new { Map1IDX = r.Map1IDX + 1, Map2IDX = r.Map2IDX + 1, Status = r.Status};

        this.DeltaMapGridView.DataSource = map;
        this.DeltaMapGridView.DataBind();

        DataTable bindingTable = new DataTable("DeltaBindingTable");
        DataColumn lineID = new DataColumn("LineID", System.Type.GetType("System.Int32"));
        DataColumn Map1Content = new DataColumn("Map1Content");
        DataColumn Map2Content = new DataColumn("Map2Content");
        DataColumn Status = new DataColumn("Status");
        bindingTable.Columns.Add(lineID);
        bindingTable.Columns.Add(Map1Content);
        bindingTable.Columns.Add(Map2Content);
        bindingTable.Columns.Add(Status);

        FillBindingTable(map1, map2, results, ref bindingTable);

        Session["DeltaBindingTable"] = bindingTable;
        ////var results = from row in compareData.AsEnumerable()
        ////              where row.Field<int>("LineID") >= 1 && row.Field<int>("LineID") < 20
        ////              select row;
        this.DeltaGridView.DataSource = bindingTable.Select("LineID >= 1 AND LineID < 20");
        this.DeltaGridView.DataBind();

    }

    private void FillBindingTable(ContentWrapper map1, ContentWrapper map2, List<DeltaResult> results, ref DataTable bindingTable)
    {
        string map1Lines = String.Empty;
        string map2Lines = String.Empty;
        int count = 1;
        System.Text.StringBuilder sourceString = new System.Text.StringBuilder();
        System.Text.StringBuilder compareString = new System.Text.StringBuilder();

        DataRow deltaRow = null;

        foreach (DeltaResult result in results)
        {
            switch (result.Status)
            {
                case DeltaResultStatus.DeleteMap1:

                    for (int i = 0; i < result.Length; i++)
                    {
                        deltaRow = bindingTable.NewRow();
                        deltaRow["LineID"] = count;
                        deltaRow["Map1Content"] = "<div class='DeleteMap1'>" + Server.HtmlEncode(((ContentWrapperLine)map1.GetByIndex(result.Map1IDX + i)).ContentLine) + "</div>";
                        bindingTable.Rows.Add(deltaRow);

                        count++;
                    }
                    break;
                case DeltaResultStatus.Match:
                    for (int i = 0; i < result.Length; i++)
                    {
                        deltaRow = bindingTable.NewRow();
                        deltaRow["LineID"] = count;
                        deltaRow["Map1Content"] = Server.HtmlEncode(((ContentWrapperLine)map1.GetByIndex(result.Map1IDX + i)).ContentLine);
                        deltaRow["Map2Content"] = Server.HtmlEncode(((ContentWrapperLine)map2.GetByIndex(result.Map2IDX + i)).ContentLine);
                        bindingTable.Rows.Add(deltaRow);

                        count++;
                    }
                    break;
                case DeltaResultStatus.AddMap2:
                    for (int i = 0; i < result.Length; i++)
                    {
                        deltaRow = bindingTable.NewRow();
                        deltaRow["LineID"] = count;
                        deltaRow["Map2Content"] = "<div class='AddMap2'>" + Server.HtmlEncode(((ContentWrapperLine)map2.GetByIndex(result.Map2IDX + i)).ContentLine) + "</div>";
                        bindingTable.Rows.Add(deltaRow);

                        count++;
                    }
                    break;
                case DeltaResultStatus.ReplaceMap1:
                    for (int i = 0; i < result.Length; i++)
                    {
                        deltaRow = bindingTable.NewRow();
                        deltaRow["LineID"] = count;
                        deltaRow["Map1Content"] = "<div class='ReplaceMap1'>" + Server.HtmlEncode(((ContentWrapperLine)map1.GetByIndex(result.Map1IDX + i)).ContentLine) + "</div>";
                        deltaRow["Map2Content"] = "<div class='ReplaceMap1'>" + Server.HtmlEncode(((ContentWrapperLine)map2.GetByIndex(result.Map2IDX + i)).ContentLine) + "</div>";
                        bindingTable.Rows.Add(deltaRow);

                        count++;
                    }
                    break;
            }
        }

    }

    protected void StartDeltaButton_Click(object sender, EventArgs e)
    {
        if (this.Map1File.FileContent != null && this.Map2File.FileContent != null)
        {
            ContentWrapper map1 = new ContentWrapper(this.Map1File.FileContent);
            ContentWrapper map2 = new ContentWrapper(this.Map2File.FileContent);

            ProcessStreams(map1, map2);
        }
    }
}